#include <HardwareSerial.h>

// 硬件配置
HardwareSerial MySerial(1);    // 自定义串口实例
const int ledPin = 4;          // 板载LED引脚（GPIO4）
int counter = 0;               // 累加计数器
const int sendInterval = 2000; // 发送间隔(ms)
const int blinkInterval = 500; // LED闪烁间隔(ms)

unsigned long previousSendMillis = 0;
unsigned long previousBlinkMillis = 0;
bool ledState = LOW;           // LED当前状态

void setup() {
  // 初始化调试串口
  Serial.begin(115200);
  
  // 初始化自定义串口
  MySerial.begin(9600, SERIAL_8N1, 0, 16); // Rx=GPIO0, Tx=GPIO16
  
  // 配置板载LED引脚
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledState); // 初始状态
}

void loop() {
  unsigned long currentMillis = millis();

  // LED闪烁控制（非阻塞）
  if (currentMillis - previousBlinkMillis >= blinkInterval) {
    previousBlinkMillis = currentMillis;
    ledState = !ledState;
    digitalWrite(ledPin, ledState);
  }

  // 串口发送控制（非阻塞）
  if (currentMillis - previousSendMillis >= sendInterval) {
    previousSendMillis = currentMillis;
    
    // 发送累加数据
    MySerial.print("Count: ");
    MySerial.println(counter);
    
    // 调试输出
    Serial.print("[Debug] Sent: ");
    Serial.println(counter);
    
    counter++;
  }
}